//
//  LocationViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/7.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "LocationViewController.h"
#import "CreateManagerViewController.h"
//#import <BaiduMapAPI_Base/BMKBaseComponent.h>
//#import <BaiduMapAPI_Map/BMKMapComponent.h>
//#import <BaiduMapAPI_Search/BMKSearchComponent.h>
//#import <BaiduMapAPI_Location/BMKLocationComponent.h>
//#import <BaiduMapAPI_Map/BMKMapView.h>
#import "Header.h"
/*
 <BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
 
 BMKCoordinateRegion region;
 
 NSInteger _isUpdate;
 }
 */
@interface LocationViewController ()

@property(nonatomic,strong) UITextField             *searchTextField;
@property(nonatomic,strong) UITableView             *addressTableView;
@property(nonatomic,strong) NSArray                 *addressArray;
@property(nonatomic,strong) UIView                  *addressView;
@property(nonatomic,strong) UIButton                *locationAdd;
@property(nonatomic,strong) UIButton                *clickAdd;
//@property(nonatomic,strong) BMKMapView              *mapView;
//@property(nonatomic,strong) BMKLocationService      *locationService;
//@property(nonatomic,strong) BMKGeoCodeSearch        *search;
//@property(nonatomic,strong) BMKGeoCodeSearch        *searcher;

@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化页面
    [self initView];
    
    // 初始化导航视图
    [self initNavigation];
}

/* - - - - - - - - - - - - - - -页面相关设置 - - - - - - - - - - - -  */

- (void) initView {
    
    [self.view addSubview:self.addressView];
    
    [self.addressView addSubview:self.locationAdd];
    
    [self.addressView addSubview:self.clickAdd];
    /*
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H - My_Height(100) - 64)];
    
    [_mapView setZoomLevel:16];
    
    [self.view addSubview:_mapView];
    
    _locationService = [[BMKLocationService alloc] init];
    
    [_locationService setDelegate:self];
    
    _search =[[BMKGeoCodeSearch alloc]init];
    _search.delegate = self;
    
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    
    _isUpdate = 0;
    
    [self startLocation];
    
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.95 alpha:1]];
     */
}

/* - - - - - - - - - - - - - - -设置导航控制器样式 - - - - - - - - - - - -  */

- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    
    // 设置导航控制器左右按钮
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
}

/* - - - - - - - - - - - - - - -点击事件相关设置 - - - - - - - - - - - -  */

//普通态
-(void)startLocation
{
    NSLog(@"进入普通定位态");
    /*
    [_locationService startUserLocationService];
    
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    
    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    
    _mapView.showsUserLocation = YES;//显示定位图层
     */
}

- (void) action_backItmeClickEvent {
    
    [[CreateManagerViewController shardManger] showLocationViewController];
}
/*
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    if (_isUpdate == 0) {
        
        _mapView.showsUserLocation = YES;//显示定位图层
        
        region.center.latitude = userLocation.location.coordinate.latitude;
        
        region.center.longitude = userLocation.location.coordinate.longitude;
        
        [_mapView setRegion:region animated:YES];
        
        [_mapView updateLocationData:userLocation];
        
        //发起反向地理编码检索
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
        
        BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        
        reverseGeoCodeSearchOption.reverseGeoPoint = pt;
        
        BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
        
        if(flag)
        {
            NSLog(@"定位反geo检索发送成功");
        }
        else
        {
            NSLog(@"定位反geo检索发送失败");
        }
        
        _isUpdate ++;
    }
}

- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi {

    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    
    reverseGeoCodeSearchOption.reverseGeoPoint = mapPoi.pt;
    
    BOOL flag = [_search reverseGeoCode:reverseGeoCodeSearchOption];
    if(flag)
    {
        NSLog(@"点击反geo检索发送成功");
    }
    else
    {
        NSLog(@"点击反geo检索发送失败");
    }
}

//接收反向地理编码结果
- (void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    
    if (searcher == _search) {
        
        if (error == BMK_SEARCH_NO_ERROR) {
            
            [self.clickAdd setTitle:result.address forState:UIControlStateNormal];
        }
        else {
            
            NSLog(@"抱歉，未找到结果");
        }
    }else {
    
        if (error == BMK_SEARCH_NO_ERROR) {
            
            [self.locationAdd setTitle:result.address forState:UIControlStateNormal];
        }
        else {
            
            NSLog(@"抱歉，未找到结果");
        }
    }
}

- (void) clickAddress : (UIButton *) clickBtn {

    if (clickBtn.titleLabel.text.length > 0) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickAddress" object:clickBtn.titleLabel.text];
        
        [[CreateManagerViewController shardManger] showLocationViewController];
    }
}
*/
#pragma mark -- 懒加载

- (UIView *)addressView {

    if (!_addressView) {
        
        _addressView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_H - My_Height(100), SCREEN_W, My_Height(100))];
        
        [_addressView setBackgroundColor:Btn_Color];
        
        UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(10), My_Height(80), My_Height(20))];
        
        [locationLabel setText:@"当前位置:"];
        
        [locationLabel setTextColor:[UIColor grayColor]];
        
        [locationLabel setFont:[UIFont systemFontOfSize:14]];
        
        [_addressView addSubview:locationLabel];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(40), SCREEN_W - My_Height(20), My_Height(0.5))];
        
        [lineView setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:1]];
        
        [_addressView addSubview:lineView];
        
        UILabel *clickLabel = [[UILabel alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(50), My_Height(80), My_Height(20))];
        
        [clickLabel setText:@"点击位置:"];
        
        [clickLabel setTextColor:[UIColor grayColor]];
        
        [clickLabel setFont:[UIFont systemFontOfSize:14]];
        
        [_addressView addSubview:clickLabel];
        
        UIView *downLine = [[UIView alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(80), SCREEN_W - My_Height(20), My_Height(0.5))];
        
        [downLine setBackgroundColor:[UIColor grayColor]];
        
        [_addressView addSubview:downLine];
        
        UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_W - My_Height(200), My_Height(80), My_Height(200), My_Height(20))];
        
        [msgLabel setFont:[UIFont systemFontOfSize:12]];
        
        [msgLabel setTextColor:[UIColor grayColor]];
        
        [msgLabel setText:@"点击对应的地址，填写到表单"];
        
        [_addressView addSubview:msgLabel];
    }
    
    return _addressView;
}

- (UIButton *)locationAdd {

    if (!_locationAdd) {
        
        _locationAdd = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [_locationAdd setFrame:CGRectMake(My_Height(70), My_Height(10), SCREEN_W - My_Height(80), My_Height(20))];
        
        [_locationAdd addTarget:self action:@selector(clickAddress:) forControlEvents:UIControlEventTouchUpInside];
        
        [_locationAdd.titleLabel setTextColor:[UIColor grayColor]];
    }
    
    return _locationAdd;
}

- (UIButton *)clickAdd {

    if (!_clickAdd) {
        
        _clickAdd = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [_clickAdd setFrame:CGRectMake(My_Height(70), My_Height(50), SCREEN_W - My_Height(80), My_Height(20))];
        
        [_clickAdd addTarget:self action:@selector(clickAddress:) forControlEvents:UIControlEventTouchUpInside];
        
        [_clickAdd.titleLabel setTextColor:[UIColor grayColor]];
    }
    
    return _clickAdd;
}

-(void)viewWillAppear:(BOOL)animated
{
    /*
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
     */
}
-(void)viewWillDisappear:(BOOL)animated
{
    /*
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
     */
}

@end
